package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Jobhunter;
import com.qf.joint.pojo.Recruitpost;
import com.qf.joint.pojo.Tradepost;
import com.qf.joint.service.TradepostService;
import com.qf.joint.utils.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/tradepost")
public class TradepostController {
    @Autowired
    private TradepostService tradepostService;
    @ResponseBody
    @RequestMapping("/category/{category}")
    public Info<Tradepost> select(@PathVariable("category")int category){
        Tradepost tradepost=new Tradepost();
        tradepost.setCategory(category);
        List<Tradepost> list = tradepostService.select(tradepost);
/*
        Tradepost insert=new Tradepost();
        insert.setName("888888");
        Tradepost update=new Tradepost();
        update.setTid(2);
        update.setName("55555555");
        List<Integer> num=new ArrayList<>();
        num.add(3);
        int i=tradepostService.delete(num);
        int y=tradepostService.insert(insert);
        int k=tradepostService.update(update);
        System.out.println(i);
        System.out.println(y);
        System.out.println(k);*/

        Info<Tradepost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());
        return info;
    }
    @ResponseBody
    @RequestMapping("/fcategory/{fcategory}")
    public Info<Tradepost> select2(@PathVariable("fcategory")int fcategory){
        Tradepost tradepost=new Tradepost();
        tradepost.setFcategory(fcategory);
        List<Tradepost> list = tradepostService.select(tradepost);
        Info<Tradepost> info=new Info<>();
        info.setData(list);
        info.setMsg("没有查到数据");
        info.setCode(0);
        info.setCount(list.size());
        return info;
    }


    /*后台分页查询*/
    @RequestMapping(value = "/all")
    @ResponseBody
    public Info<Tradepost> selectAll(@RequestParam(defaultValue = "5") Integer page,
                                     @RequestParam(defaultValue = "5") Integer limit) {
        //int count = jobhunterService.selectAll().size();
        PageHelper.startPage(page,limit);
        List<Tradepost> tradeposts = tradepostService.selectAll();
        PageInfo<Tradepost> pageInfo =new PageInfo<>(tradeposts);
        Info<Tradepost> info = new Info<Tradepost>();
        if(tradeposts!=null&tradeposts.size()>0){
            info.setCode(0);
            info.setCount((int) pageInfo.getTotal());
            info.setData(pageInfo.getList());
            info.setMsg("success");
            return info;
        }
        info.setCode(1);
        info.setMsg("err");
        return info;
    }

    /*后台删除*/
    @RequestMapping(value="/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(name = "idArry", required = true) String idArry){
        String[] idlist = idArry.split(",");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < idlist.length; i++) {
            list.add(idlist[i]);
        }
        int res = tradepostService.delete(list);
        System.out.println("delete ine");
        return "redirect:/admin/admin";
    }

    /*后台新增*/

    @RequestMapping(value = "/insertOne", method = RequestMethod.POST)
    public String insertOne(Tradepost tradepost) {
        int insertOne = tradepostService.insert(tradepost);
        System.out.println(insertOne);
        return "redirect:/admin/admin";
    }

    /*后台修改*/
    @RequestMapping(value = "/updateOne", method = RequestMethod.POST)
    @ResponseBody
    public Info<Tradepost> updateOne(Tradepost tradepost) {
        int update = tradepostService.update(tradepost);
        Info<Tradepost> info = new Info<Tradepost>();
        info.setCode(update);
        /*if (update == 1) {
            return "修改成功";
        }
        return "修改失败";*/
        return info;

    }
}
